/*****************************************************************************
 * kal_freertos_timer.c
 *
 * Copyright (C) 2019 Jeasonvor <1101627719@qq.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

/*****************************************************************************
 * Included Files
 ****************************************************************************/

#include "plum_api.h"
#include "kal/source/kal_freertos/kal_freertos.h"

/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/
#define LOG_RUN_LEVEL LOG_LEVEL_DEBUG
#define LOG_MODULE    "kal.timer"
#include "thirdparty/log/log.h"
/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/*****************************************************************************
 * Private Types
 ****************************************************************************/
typedef struct {
    plum_s8       name[PLUM_KAL_TIMER_NAME_MAX];
    TimerHandle_t hdl;
    plum_void (*func)(plum_void *arg);
    plum_void *      arg;
    struct list_head list;
} kal_timer_t;

/*****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/*****************************************************************************
 * Private Data
 ****************************************************************************/
PLUM_PRIVATE kal_timer_t timer_head;
/*****************************************************************************
 * Public Data
 ****************************************************************************/

/*****************************************************************************
 * Private Functions
 ****************************************************************************/

PLUM_PRIVATE
kal_timer_t *kal_timer_apply(PLUM_CONST plum_s8 *name, plum_void (*fn)(void *),
                             void *              arg)
{
    kal_timer_t *node = plum_null;

    do {
        if (!name) {
            LOG_E("kal_time_apply parameter is null");
            break;
        }

        node = (kal_timer_t *)KAL_FREERTOS_MALLOC(sizeof(kal_timer_t));
        if (!node) {
            LOG_E("time malloc err");
            break;
        }

        memset(node, 0, sizeof(kal_timer_t));

        snprintf((char *)node->name, sizeof(node->name), "%s", name);
        node->func = fn;
        node->arg  = arg;
    } while (0);

    return (node);
}

PLUM_PRIVATE
plum_s32 kal_timer_del(kal_timer_t *node)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!node) {
            rc = PLUM_ECODE_EPARA;
            break;
        }

        list_del(&node->list);

        KAL_FREERTOS_FREE(node);
    } while (0);

    return (rc);
}

PLUM_PRIVATE
plum_s32 kal_timer_add(kal_timer_t *node)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!node) {
            LOG_E("kal_time_add param is null");
            rc = PLUM_ECODE_EMEM;
            break;
        }

        list_add(&node->list, &timer_head.list);
    } while (0);

    return (rc);
}

PLUM_PRIVATE
kal_timer_t *kal_timer_find(PLUM_CONST plum_s8 *name)
{
    kal_timer_t *node = plum_null;

    struct list_head *pos;
    list_for_each(pos, &timer_head.list)
    {
        node = list_entry(pos, kal_timer_t, list);
        if (strcmp((const char *)name, (const char *)node->name) == 0) {
            break;
        }
    }

    return (node);
}

PLUM_PRIVATE
void kal_timer_working(TimerHandle_t xTimer)
{

    pvTimerGetTimerID(xTimer);
}
/*****************************************************************************
 * Public Functions
 ****************************************************************************/

PLUM_PUBLIC
plum_s32 plum_kal_timer_create(PLUM_CONST plum_s8 *name,
                               plum_void (*fn)(plum_void *arg), plum_void *arg,
                               plum_u32 period, plum_kal_timer_mode_t mode)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!name) {
            rc = PLUM_ECODE_EPARA;
            break;
        }

        kal_timer_t *node = kal_timer_apply(name, fn, arg);
        if (!node) {
            LOG_E("kal_time_apply err");
            rc = PLUM_ECODE_EMEM;
            break;
        }

        node->hdl = xTimerCreate(name, period, pdTRUE, node, kal_timer_working);
        if (node->hdl) {
            LOG_E("xTimerCreate err");
            rc = plum_kal_timer_delete(name);
            break;
        }

        rc = kal_timer_add(node);
        if (rc) {
            LOG_E("kal_timer_add err,rc:%d", rc);
            break;
        }

        PLUM_UNUSED(mode);
        LOG_I("create timer,named:%s", name);
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_kal_timer_delete(PLUM_CONST plum_s8 *name)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!name) {
            rc = PLUM_ECODE_EPARA;
            break;
        }

        kal_timer_t *node = kal_timer_find(name);
        if (!node) {
            LOG_E("not fined the timer,named:%d", name);
            rc = PLUM_ECODE_EPARA;
            break;
        }

        if (node->hdl) {
            xTimerDelete(node->hdl, 0);
        }

        rc = kal_timer_del(node);
        if (rc) {
            LOG_E("kal_timer_del err,rc:%d", rc);
            break;
        }

        LOG_I("delete timer,named:%s", name);
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_u32 plum_kal_timer_suspend(PLUM_CONST plum_s8 *name)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!name) {
            rc = PLUM_ECODE_EPARA;
            break;
        }

        kal_timer_t *node = kal_timer_find(name);
        if (!node) {
            LOG_E("not find the timer,named:%s", name);
            rc = PLUM_ECODE_EPARA;
            break;
        }

        rc = xTimerStop(node->hdl, 0);
        if (rc == pdFAIL) {
            LOG_E("suspend timer[%s] err,rc:%d", name, rc);
            rc = PLUM_ECODE_EIO;
            break;
        }

        LOG_I("timer[%s] have suspended", name);
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_kal_timer_resume(PLUM_CONST plum_s8 *name)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!name) {
            rc = PLUM_ECODE_EPARA;
            break;
        }

        kal_timer_t *node = kal_timer_find(name);
        if (!node) {
            LOG_E("not find the timer,named:%s", name);
            rc = PLUM_ECODE_EPARA;
            break;
        }

        rc = xTimerStart(node->hdl, 0);
        if (rc) {
            LOG_E("resume timer[%s] err,rc:%d", name, rc);
            rc = PLUM_ECODE_EIO;
            break;
        }

        LOG_I("timer[%s] have resumed", name);
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_void plum_kal_timer_show(PLUM_CONST plum_s8 *name)
{
    if (name) {
        kal_timer_t *node = kal_timer_find(name);
        if (node) {
            LOG_I("time [%p] named:%d", node->hdl, node->name);
        }
    }
    else {
        struct list_head *pos;
        list_for_each(pos, &timer_head.list)
        {
            kal_timer_t *node = list_entry(pos, kal_timer_t, list);
            LOG_I("time [%p] named:%d", node->hdl, node->name);
        }
    }
}

/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
